<?php
// TODO: pom    澳买拼团 逻辑处理
namespace Home\Common\Api;

class Images
{

    public $compress_url = null;

    // TODO: 上传图片
    public function upload_img($type_id)
    {
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 1000000000;
      $upload->rootPath = './Public/upload/';
      $upload->savePath = '';
      $upload->saveName = array('uniqid','');
      $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
      $upload->autoSub  = true;
      $upload->subName  = array('date','Ymd');
       // 上传文件
      $info   =   $upload->upload($_FILES);

      //return false;
      if(!$info) {// 上传错误提示错误信息
      echo $upload->getError();
      //echo $targetPath;
      }
           $images = new \Think\Image();
           $images->open('./Public/upload/'.$info['file']['savepath'].$info['file']['savename']);
           $newsavename = str_replace('.','_thumb.',$info['file']['savename']);
           $wid = 240;
           $hei = 240;
           $vv = $images->thumb($wid, $hei,\Think\Image::IMAGE_THUMB_FIXED)->save('./Public/upload/'.$info['file']['savepath'].$newsavename);

           $info['file']['savepath_thumb'] = "./Public/upload/".$info['file']['savepath'];//缩略图显示路径
           $info['file']['savethumbname_thumb'] = $newsavename;
         foreach($info as $file){
            // echo   '/upload/'.$file['savepath'].$file['savethumbname_thumb'];
             $data['url'] = "/upload/".$file['savepath'].$file['savename'];
             $data['thumb_url'] = "/upload/".$file['savepath'].$file['savethumbname_thumb'];
             $data['type_id'] = $type_id ;

             $res  =  $this->insert_images($data);
           }
           return $res;
    }
    // TODO: 单图上传图片
    public function one_upload_img()
    {
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 1000000000;
      $upload->rootPath = './Public/upload/';
      $upload->savePath = '';
      $upload->saveName = array('uniqid','');
      $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
      $upload->autoSub  = true;
      $upload->subName  = array('date','Ymd');
       // 上传文件
      $info   =   $upload->upload($_FILES);

      //return false;
      if(!$info) {// 上传错误提示错误信息
      echo $upload->getError();
      //echo $targetPath;
      }
           $images = new \Think\Image();
           $images->open('./Public/upload/'.$info['file']['savepath'].$info['file']['savename']);
           $newsavename = str_replace('.','_thumb.',$info['file']['savename']);
           $wid = 240;
           $hei = 240;
           $vv = $images->thumb($wid, $hei,\Think\Image::IMAGE_THUMB_FIXED)->save('./Public/upload/'.$info['file']['savepath'].$newsavename);

           $info['file']['savepath_thumb'] = "./Public/upload/".$info['file']['savepath'];//缩略图显示路径
           $info['file']['savethumbname_thumb'] = $newsavename;
         foreach($info as $file){
            // echo   '/upload/'.$file['savepath'].$file['savethumbname_thumb'];
             // $data['url'] = "/upload/".$file['savepath'].$file['savename'];
             $data['thumb_url'] = "/upload/".$file['savepath'].$file['savethumbname_thumb'];
             // $res  =  $this->insert_images($data);
           }
           return $data;
    }




    // TODO: 将图片路径保存数据库
    public function insert_images($data)
    {
   
      $map = array();
      $map['u_id'] = $_SESSION['user_id'];
      $map['addtime'] = date('Y-m-d H:i:s');
      $map['sort'] = 0;
      $map['type_id'] = $data['type_id'];
      $map['title'] = '';
      $map['content'] = '';
      $map['happen_time'] = 0;
      $map['is_activity'] = 1;
      $map['raw_url'] = $data['url'];
      $map['compress_url'] = $data['thumb_url'];
      $res = D('images')->add($map);
      if($res){
            $this->compress_url = $map['compress_url'];
            return $res;
      }
    }

    // TODO: 将图片删除
    public function del_img($ids)
    {
      $where = "id in (".$ids.")";
      if(empty($where)){
        $res = D('Images')->where($where)->delete();
          if($res){
                return $res;
          }
      }

    }

    // TODO: 添加相册
    public function add_photo($data)
    {
      $data['u_id'] = $_SESSION['user_id'];
      $res = D('type')->add($data);
      if($res){
            return $res;
      }
    }
        // TODO: 查看相册
    public function sel_photo($data)
    {
      //取出该用户的图片
      $data = array();
      $pagesize= empty($ary['pagesize'])?8:$ary['pagesize'];
      $pagenow = empty($ary['pagenow'])?1:$ary['pagenow'];
      $where['u_id'] = $_SESSION['user_id'];
      $where['is_activity'] = 1;
      $count = D('Type')->where($where)->count();
      $pagecount = ceil($count/$pagesize);
      $totalPages = ($pagenow -1)*$pagesize;
      $data['list'] = D('Type')->where($where)->limit($totalPages,$pagesize)->order('id desc')->select();

      $data['page'] = array('page'=>$ary['pagenow'],'pagesize'=>$ary['pagesize'],'count'=>$count,'pagecount'=>$pagecount);
      if($data){
        return $data;
      }
    }



//绑定相册图片
    public function  photo_add_img2($type_id){
       $type_id = $type_id;

       $img_id = $this->upload_img($type_id );
       if($img_id == ''){
        return '';
       }
       //查看该相册是否有图片，没有就默认上传的第一张图片位相册图片
       $photo = D('type')->field('img')->where('id = '.$type_id)->find();
       if($photo == null || $photo['img'] == ""){
            //保存压缩图
        $img['img'] = $this->compress_url;

        if($img['img']){
          $res = D('type')->where('id = '.$type_id)->save($img);
          if(!$res){
            return "";
          }
        }

       }
       return $img_id;
/*       $where['u_id'] =$_SESSION['user_id'];
       $where['id'] = $img_id;     
       $save['type_id'] = $data['type_id'];
       $img_list = D('Images')->where($where)->save($save);
       if($img_list){
          return $img_list;
        }*/

    }
 

public function sel_photo_img($type_id){
   $where = "type_id = ".$type_id." and u_id=".$_SESSION['user_id'];
   $img_list = D('Images')->where($where)->select();
          if($img_list){
          return $img_list;
        }

     return '';
   }









}






?>
